﻿
$c.fn.pager = function (config) {
    var _this = $(this.jo);
    //var elPager = $(this.jo);
    var obj = function () {
        this.config = $.extendEx({}, this.defConfig, config);
    }
    obj.prototype.defConfig = {
        pageNo: 1,
        pageSize:15,
        numPagerItemCount: 10,
        pageTotal: 1,
        //showPreNext: true,
        prevPageText: "上一页",
        nextPageText: "下一页",
        //showFirstLast: true,
        firstPageText: "首页",
        lastPageText: "尾页",
        //showNumericPagerItems: true,
        pagerItemClick: null,
        pagerItems:'page-size-dropdown,|,first,pre,|,goto,|,num,|,next,last,|,load,remark'
    }
    //var defOpt = {
        
    //};
    //var opt = $.extend(defOpt, options);

    //opt.pageNo = parseInt(opt.pageNo, 10);
    //opt.pagerItemCount = parseInt(opt.numericPagerItemCount, 10);
    //opt.pageTotal = parseInt(opt.pageTotal, 10);
    //elPager.options = opt;
    obj.prototype.addSeparator = function () {
        _this.append('<a class="kui-pager-sep"><i></i></a>');
    }
    obj.prototype.addPagerItem = function (text, pageNo, enabled, type) {
        var _obj = this;
        
        var itemHtml = "<a p='" + pageNo + "'><span>" + text + "</span></a>";
        
        if (type == "num" && pageNo == _ob.config.pageNo) {
            itemHtml = "<strong p='" + pageNo + "'><span>" + text + "</span></strong>";
        }
        var pagerItem = $(itemHtml).appendTo(_this);
        pagerItem.click(function () {
            if (!enabled) {
                return;
            }
            var _pageNo = parseInt($(this).attr("p"), 10);
            if ($.isFunction(_obj.config.pagerItemClick)) {
                _obj.config.pagerItemClick(_pageNo,pagerItem);
            }
            $c(_this).pager($.extend(_obj.config, { pageNo: _pageNo }));
        });
        if (type == "num" && pageNo == _obj.config.pageNo) {
            pagerItem.addClass("kui-pager-item-curr");
        }
        //_this.append(pagerItem);
        pagerItem.addClass('kui-pager-item-'+type+'');
        if (!enabled) {
            pagerItem.addClass('kui-pager-item-disabled');
        }
    };
    obj.prototype.addFirst = function () {
        this.addPagerItem(this.config.firstPageText, 1, this.config.pageNo > 1, 'first');
    }
    obj.prototype.addPre = function () {
        this.addPagerItem(this.config.prevPageText, this.config.pageNo-1, this.config.pageNo > 1, 'pre');
    }
    obj.prototype.addNumeric = function (page) {
        this.addPagerItem(page, page, true, 'num');
    }
    obj.prototype.addNext = function () {
        this.addPagerItem(this.config.nextPageText, this.config.pageNo + 1, this.config.pageNo < this.config.pageTotal, 'next');
    }
    obj.prototype.addLast = function () {
        this.addPagerItem(this.config.lastPageText, this.config.pageTotal, this.config.pageNo < this.config.pageTotal, 'last');
    }
    obj.prototype.addGoto = function () {
        var element = $('<a class="kui-pager-item-goto"><span>第</span><input type="text" class="kui-pager-item-goto-textbox"/><span>共</span><span class="kui-pager-item-goto-pagetotal">' + this.config.pageTotal + '</span><span>页</span></a>').appendTo(_this);
        var input = element.find('input');
        input.val(_obj.config.pageNo);
        input.keydown(function (e) {
            var key = e.which;
            if (key == 13) {
                if ($.isFunction(_obj.config.pagerItemClick)) {
                    _obj.config.pagerItemClick(parseInt(input.val(),10),input);
                }
            }
        });

    }
    obj.prototype.addRefresh = function () {
        var element = $('<a class="kui-pager-item-load"><span></span></a>').appendTo(_this);
        element.click(function () {
            if ($.isFunction(_obj.config.pagerItemClick)) {
                _obj.config.pagerItemClick(_obj.config.pageNo,element);
            }
        });
    }
    obj.prototype.addPageSizeDropdown = function () {
        var _obj=this;
        var ddl = $('<div class="kui-pager-pagesize-dropdown" style="display:inline-block;"></div>').appendTo(_this);
        var _data = [];
        if (_obj.config.pageList) {
            $.each(_obj.config.pageList, function (i, p) {
                _data.push({text:p,value:p});
            });
        }
        form(ddl).select({
            name: 'kui-pager-pagesize-dropdown',
            data: _data,
            value: _obj.config.pageSize,
            onChange: function (elem, o) {
                //alert('已选中：' + o.text + "，值为：" + o.value);
                if (_obj.config.onPageSizeChange) {
                    _obj.config.onPageSizeChange(elem, o);
                }
            }
        });
    }
    obj.prototype.addRemark = function (remark) {
        var _obj = this;
        var element = $('<div class="kui-pager-item-remark" style="display:inline-block;"></div>').appendTo(_this);
        element.html(remark);
    }
    _this.addClass('kui-pager');
    _this.html("");

    var _obj = new obj();
    var pagerItemArray = _obj.config.pagerItems.split(',');
    $.each(pagerItemArray, function (i, type) {
        switch (type) {
            case 'first':
                _obj.addFirst();
                break;
            case 'pre':
                _obj.addPre();
                break;
            case 'num':
                var _startpageNo = _obj.config.pageNo - (_obj.config.numPagerItemCount / 2);
                if (_startpageNo + _obj.config.numPagerItemCount > _obj.config.pageTotal)
                    _startpageNo = _obj.config.pageTotal + 1 - _obj.config.numPagerItemCount;
                if (_startpageNo < 1)
                    _startpageNo = 1;
                var _endpageNo = _startpageNo + _obj.config.numPagerItemCount - 1;
                if (_endpageNo > _obj.config.pageTotal)
                    _endpageNo = _obj.config.pageTotal;
                for (var i = _startpageNo; i <= _endpageNo; i++) {
                    _obj.addNumeric(i);
                }
                break;
            case 'next':
                _obj.addNext();
                break;
            case 'last':
                _obj.addLast();
                break;
            case '|':
                _obj.addSeparator();
                break;
            case 'goto':
                _obj.addGoto();
                break;
            case 'load':
                _obj.addRefresh();
                break;
            case 'page-size-dropdown':
                _obj.addPageSizeDropdown();
                break;
            //case 'remark':
            //    _obj.addRemark();//外部手动调用.
            //    break;
            default:
                break;
        }
    });
    return _obj;
};